package exhi.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.stp.core.entity.IGridRowFactory;
import com.stp.core.entity.XGrid;
import com.stp.core.entity.XGridPage;
import com.stp.core.entity.XGridRow;
import com.stp.core.entity.XMsg;
import com.stp.core.spring3.BaseSpringAction;

import exhi.entity.EntpFepr;
import exhi.manager.EntpFeprMng;

@Controller
@RequestMapping("/entp")
public class EntpFeprAct extends BaseAct {
	
	private static final String PATH_TABLE = "/fepr-table";
	private static final String PATH_LIST = "/fepr-list";
	private static final String PATH_DETAIL = "/fepr-detail";
	private static final String PATH_SAVE = "/fepr-save";
	private static final String PATH_DELETE = "/fepr-delete";
	
	// 新展可行性方案
	@RequestMapping(PATH_TABLE)
	public void table(
			@RequestParam(value = "code", required = true, defaultValue = "") String code,
			ModelMap model) {
		model.addAttribute("code", code);

	}
	
	// 权限json数据
	@RequestMapping(PATH_LIST)
	@ResponseBody
	public String infoList(XGridPage page,
			@RequestParam("cap_code") String capCode,
			@RequestParam("cap_name") String capName)
			throws InstantiationException, IllegalAccessException {

		String sql = "from EntpFepr info where 1=1";
		sql += sqlModel.getSQLChars("info.code", "=", capCode, false, "and");
		sql += sqlModel.getSQLChars("info.zlhmc", "like", capName, false,
				"like");
		sql += " order by info.createtime desc";
		InfoGridRow row = new EntpFeprAct.InfoGridRow();
		XGrid grid = entpFeprMng.getXGrid(sql, row, page.getPageNo(),
				page.getPageSize(), false);
		if (null == grid)
			return "";
		return "{\"xGrid\":" + toJson(grid) + "}";
	}
	
	// 明细数据
	@RequestMapping(PATH_DETAIL)
	public void infoDetail(
			@RequestParam(value = "code", required = true, defaultValue = "") String code,
			@ModelAttribute("entpFepr") EntpFepr entpFeprInfo,
			ModelMap model) {
		model.addAttribute("code", code);
		model.addAttribute("info", entpFeprInfo);

	}
	
	// 数据保存
	@RequestMapping(PATH_SAVE)
	@ResponseBody
	public String saveInfo(
			HttpServletRequest request,
			@ModelAttribute("entpFepr") EntpFepr entpFeprInfo,
			ModelMap model) throws IllegalStateException, IOException {
		entpFeprInfo.setCreatetime(new Date());
		entpFeprMng.save(entpFeprInfo);
		XMsg msg = new XMsg();
		String entpId = entpFeprInfo.getId();
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		//新展可行性方案--银行存款证明&上年度资产证明（审计）
		if  (saveUpload(multipartRequest, "attach1")){			
			attachMng.saveFileAttach(entpId, "YHCKZM");//银行存款证明
		}else{			
			msg.setState(false);
			msg.setMsg(attachMng.getErrors());
		}		
		if  (saveUpload(multipartRequest, "attach2")){
		
			attachMng.saveFileAttach(entpId, "SNDZCZM");//上年度资产证明（审计）
		}else{			
			msg.setState(false);
			msg.setMsg(attachMng.getErrors());
		}
			
		msg.setState(true);
		return "redirect:" + PATH_TABLE.substring(1);
	}
	
	@RequestMapping(PATH_DELETE)
	@ResponseBody
	public String infoDelete(
			@ModelAttribute("entpFepr") EntpFepr entpFeprInfo,
			ModelMap model) {
		XMsg msg = new XMsg();
		msg.setState(true);
		entpFeprMng.delete(entpFeprInfo);
		return toJson(msg);
	}
	
	@ModelAttribute("entpFepr")
	public EntpFepr getEntpSexhiInfo(
			@RequestParam(value = "infoId", required = true, defaultValue = "") String id) {
		EntpFepr entpFepr = entpFeprMng.get(id);
		if (null == entpFepr) {
			entpFepr = new EntpFepr();
		}
		return entpFepr;
	}
	
	class InfoGridRow implements IGridRowFactory {
		public XGridRow builderXRow(Object obj) {
			// TODO Auto-generated method stub
			XGridRow xrow = null;
			if (obj instanceof EntpFepr) {
				xrow = new XGridRow();
				EntpFepr info = (EntpFepr) obj;
				xrow.setId(info.getId());
				List t = new ArrayList();
				t.add("");
				t.add(info.getZlhmc());
				t.add(info.getXmjfys());//项目预算经费
				t.add(info.getYear());
				t.add("<a href='fepr-detail.jspx?infoId=" + info.getId()
						+ "&code=" + info.getCode() + "'>【编辑】</a>");
				xrow.setData(t);
			}
			return xrow;
		}
	}

	
	@Autowired
	@Qualifier("entpFeprMng")
	private EntpFeprMng entpFeprMng;
}
